//
//  PublicWebViewController.swift
//  maosheng
//
//  Created by lxl on 2019/8/14.
//

import UIKit
import WebKit
class PublicWebViewController: RootViewController {
    var webURLStr = ""
    var parameters: [String : String]?
    
    var webView = WKWebView()
    var progressWebView = UIProgressView()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addTheWeView()
        // Do any additional setup after loading the view.
    }
    // MARK: - designated init
    func addTheWeView() {
//        self.webURLStr = webURLStr
//        self.parameters = parameters
        self.webView = WKWebView()
        self.progressWebView = UIProgressView()
        //custom ui
        setupUI()
    }
    // MARK: - private
    private func setupUI() {
        //设置point&size
        self.progressWebView.frame = CGRect(x: 0.0, y: 0.0, width: SCREENW, height: 2.0)
        self.webView.frame = CGRect(x: 0.0, y: 0.0, width: SCREENW, height: SCREENH)
        
        view.addSubview(self.webView)
        view.addSubview(self.progressWebView)
        //添加监听webView网页加载进度
        self.webView.addObserver(self, forKeyPath: NSStringFromSelector(#selector(getter: webView.estimatedProgress)), options: .new, context: nil)
        //处理参数
        let requestUrl: String
        if let parameters = self.parameters {
            requestUrl = self.urlWith(parameters, baseurl: self.webURLStr)
        } else {
            requestUrl = self.webURLStr
        }
        //加载webView
        self.webView.load(URLRequest(url: URL(string: requestUrl)!))
    }
    //添加观察者方法
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        
        //设置进度条
        if keyPath == "estimatedProgress"{
            progressWebView.alpha = 1.0
            progressWebView.setProgress(Float(webView.estimatedProgress), animated: true)
            if webView.estimatedProgress >= 1.0 {
                UIView.animate(withDuration: 0.3, delay: 0.1, options: .curveEaseOut, animations: {
                    self.progressWebView.alpha = 0
                }, completion: { (finish) in
                    self.progressWebView.setProgress(0.0, animated: false)
                })
            }
        }else{
            super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
        }
            
         
    }
    private func urlWith(_ dictionary: [String : String], baseurl url: String) -> String {
        var i = 0
        var resultUrl = url
        for (key, value) in dictionary {
            resultUrl = resultUrl + (i == 0 ? "?":"&") + key + "=" + value
            i += 1
        }
        return resultUrl
    }
    
    deinit {
        self.webView.removeObserver(self, forKeyPath: NSStringFromSelector(#selector(getter: webView.estimatedProgress)))
    }
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
